#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json
from utils.ShellUtil import ShellUtil
from utils.DateUtil import DateUtil

class ConfAd() :

    def __init__(self, logger, mqttClient, sn) :
        self.logger = logger
        self.mqttClient = mqttClient
        self.sn = sn

    def download(self, message) :

        ShellUtil.execute(self.logger, "mkdir -p /www/etv_new/")

        callback = {"success": "true", "sn": self.sn, "result": []}
        for url in message["downloadUrls"] :
            command = "wget -P /www/etv_new/ " + url
            results = ShellUtil.execute(self.logger, command)

        ShellUtil.execute(self.logger, "mv /www/etv/ /www/etv%s/" % DateUtil.getNowTimeStamp())
        ShellUtil.execute(self.logger, "mv /www/etv_new/ /www/etv/" % DateUtil.getNowTimeStamp())
        self.mqttClient.publish("CONFIG_AD_CALLBACK", json.dumps(callback, ensure_ascii=False), 2)

# {
# 	"downloadUrls": [
# 		"http://101.132.161.95/file/group/1/26/service_img.zip",
# 		"http://101.132.161.95/file/group/1/26/hotel_service.json",
# 		"http://101.132.161.95/file/group/1/26/hotel_update.json",
# 		"http://101.132.161.95/file/device/MACC220180811/root.json"],
# 	"eventType": "CONFIG_AD",
# 	"publishTime": 1536155421144,
# 	"sn": "MACC220180811"
# }
